package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kgc.scd.bean.*;
import com.kgc.scd.mapper.CartMapper;
import com.kgc.scd.mapper.CategoriesMapper;
import com.kgc.scd.mapper.CategoryMapper;
import com.kgc.scd.mapper.GoodsMapper;
import com.kgc.scd.service.CategoriesInfoService;
import com.kgc.scd.service.GoodsInfoCommentService;
import com.kgc.scd.vo.ShopCarouselVO;
import com.kgc.scd.vo.ShopCategoriesVO;
import com.kgc.scd.vo.ShopCategoryVO;
import com.kgc.scd.vo.ShopGoodsVO;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created On : 2022/11/16.
 * <p>
 * Author : 22166
 * <p>
 * Description: 查询商品分类接口实现类
 */
@Service
@Slf4j
public class CategoriesInfoServiceImpl implements CategoriesInfoService {

    @Autowired(required = false)
    private CategoriesMapper categoriesMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private GoodsMapper goodsMapper;

    @Autowired(required = false)
    private CartMapper cartMapper;

    @Override
    public int getShopCartTotal(String userId) {
        log.info("---{}",userId);
        QueryWrapper<ShopCart> shopCartQueryWrapper = new QueryWrapper<>();

        shopCartQueryWrapper.eq("user_id",userId);

        int count = cartMapper.selectCount(shopCartQueryWrapper).intValue();

        log.info("111111---{}",count);
        return count;
    }

    @Override
    public List<ShopCategoriesVO> categoriesList(Integer sumCategory) {

        QueryWrapper<ShopCategories> shopCategoriesQueryWrapper = new QueryWrapper<>();
        if (sumCategory !=null){
            shopCategoriesQueryWrapper.likeRight("sum_category",sumCategory);
        }


        List<ShopCategoriesVO> shopCategoriesVOS = categoriesMapper.selectList(shopCategoriesQueryWrapper).stream().map(shopCategories -> {
            ShopCategoriesVO shopCategoriesVO =new ShopCategoriesVO();
            BeanUtils.copyProperties(shopCategories,shopCategoriesVO);
            return shopCategoriesVO;
        }).collect(Collectors.toList());

        return shopCategoriesVOS;
    }

    @Override
    public List<ShopCategoryVO> categoryList(Integer sumCategory) {

        QueryWrapper<ShopCategory> shopCategoryQueryWrapper = new QueryWrapper<>();

        shopCategoryQueryWrapper.likeRight("sum_category",sumCategory);

        List<ShopCategoryVO> shopCategoryVOList = categoryMapper.selectList(shopCategoryQueryWrapper).stream().map(shopCategory -> {
            ShopCategoryVO shopCategoryVO =new ShopCategoryVO();
            BeanUtils.copyProperties(shopCategory,shopCategoryVO);
            return shopCategoryVO;
        }).collect(Collectors.toList());

        return shopCategoryVOList

                ;
    }

    @Override
    public List<ShopGoodsVO> shopGoodsVO(String categoryId) {

        QueryWrapper<ShopGoods> shopGoodsQueryWrapper = new QueryWrapper<>();
        shopGoodsQueryWrapper.likeRight("category_id",categoryId);


        // 处理返回的结果列表，将状态进行转换为描述
        List<ShopGoodsVO> shopGoodsVOList = goodsMapper.selectList(shopGoodsQueryWrapper).stream().map(shopGoods -> {
            // 创建VO实体对象
            ShopGoodsVO shopGoodsVO = new ShopGoodsVO();
            // 拷贝实体属性
            BeanUtils.copyProperties(shopGoods, shopGoodsVO);

            return shopGoodsVO;
        }).collect(Collectors.toList());

        return shopGoodsVOList;
    }
}